package com.yawn.zdweb.controller;

import com.yawn.zdweb.entity.Group;
import com.yawn.zdweb.entity.User;
import com.yawn.zdweb.util.Constant;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Created by yawn on 2018-01-27 13:07
 */
@RestController
public class IdentityController {

    @GetMapping("/index")
    public void index() {
        // 用来跳转页面
    }

    @GetMapping("/principle")
    public User principleId(HttpSession session) {
        return (User) session.getAttribute("user");
    }

    @GetMapping("/is/manager")
    public boolean isManager(HttpSession session) {
        return listGroup(session).contains(Constant.GROUP_MANAGER);
    }

    @GetMapping("/is/developer")
    public boolean isDeveloper(HttpSession session) {
        return listGroup(session).contains(Constant.GROUP_DEVELOPER);
    }

    @GetMapping("/is/tester")
    public boolean isTester(HttpSession session) {
        return listGroup(session).contains(Constant.GROUP_TESTER);
    }

    @GetMapping("/is/require")
    public boolean isRequire(HttpSession session) {
        return listGroup(session).contains(Constant.GROUP_REQUIRE);
    }

    @GetMapping("/list/group")
    private List<String> listGroup(HttpSession session) {
        List<Group> groupList = (List<Group>) session.getAttribute("groupList");
        List<String> groupNameList = new ArrayList<>();
        if (groupList != null) {
            for (Group group : groupList) {
                groupNameList.add(group.getId());
            }
        }
        return groupNameList;
    }

}
